package com.ltmall.api.manager.impl;

import com.ltmall.api.bean.*;
import com.ltmall.api.dao.impl.GoodsDAO;
import com.ltmall.api.entity.*;
import com.ltmall.api.manager.GoodsManager;
import com.ltmall.api.parse.GoodsParse;
import com.ltmall.api.util.PageUtil;
import com.wuwangkeji.homeflavor.comm.Path;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GoodsManagerImpl implements GoodsManager {
    private GoodsDAO gDAO;

    public GoodsDAO getgDAO() {
        return gDAO;
    }

    public void setgDAO(GoodsDAO gDAO) {
        this.gDAO = gDAO;
    }

    public List<Integer> getShopID(String province, String city, String county) {
        // TODO Auto-generated method stub
        if (county.equals(""))
            county = null;
        return gDAO.getShopID(province, city, county);

    }

    public List<?> goodsList(int shopID, int type, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.goodsList(shopID, type, page);
    }

    public List<?> goodsList(int type, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.goodsList(type, page);
    }

    public List<HotCity> fiveHotCity() {
        // TODO Auto-generated method stub
        return gDAO.fiveHotCity();
    }

    public Goods goodsDetail(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.goodsDetail(goodsID);
    }

    public GoodsServer goodsServer(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.goodsServer(goodsID);
    }

    public Shop shopDetail(int shopID) {
        // TODO Auto-generated method stub
        return gDAO.shopDetail(shopID);
    }

    public List<?> brandGoods(int brandID, int type, PageUtil page) {
        // TODO Auto-generated method stub

        /*
         * 1、综合 2、销量 3/4、价格 5、评价
         */
        return gDAO.brandGoods(brandID, type, page);
    }

    public List<Goods> hotGoods(String province, String city, String county, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.topGoos(province, city, county, page);
    }

    public List<StoreHouse> getStoreHouses(int userID) {
        // TODO Auto-generated method stub
        return gDAO.getStoreHouse(userID);
    }

    public Shop shopByBussiness(int bussinessID) {
        // TODO Auto-generated method stub
        return gDAO.shopDetailByBussiness(bussinessID);
    }

    public List<?> pingGoods(List<Integer> shopIDList, int goodsID, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.pingGoods(shopIDList, goodsID, page);
    }

    public GoodsMore goodsMore(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.goodsMore(goodsID);
    }

    public List<?> latestComment(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.latestComment(goodsID);
    }

    public List<?> commentList(int goodsID, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.commentList(goodsID, page);
    }

    public List<ADS> ads(int type, int number) {
        // TODO Auto-generated method stub
        return gDAO.ads(type, number);
    }

    public GoodsStory goodsStory(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.story(goodsID);
    }

    @Override
    public ThGroupBuyingActivity groupBuyingActivityGet(int groupType) {
        return gDAO.groupBuyingActivityGet(groupType);
    }

    public Activity latestActivity() {
        // TODO Auto-generated method stub
        Activity activity = gDAO.runningActivtiy();
        if (activity != null) {
            return activity;
        } else {
            return gDAO.goingRunningActivtiy();
        }
    }

    public List<BussinessADS> shopADS(int number, int bussinessID) {
        // TODO Auto-generated method stub
        return gDAO.shopADS(number, bussinessID);
    }

    public int getBussiness(String province, String city, String county, int type) {
        // TODO Auto-generated method stub
        if (county == null || county.equals("")) {
            county = null;
        }
        return gDAO.getBussinessID(province, city, county, type);
    }

    public void updateGoodsNumber(int goodsID, int deleteNumber) {
        // TODO Auto-generated method stub
        gDAO.updateGoodsNumber(goodsID, deleteNumber);
    }

    public void updateSellOut(int goodsID, int sellOutNumber) {
        // TODO Auto-generated method stub
        gDAO.updateSellOut(goodsID, sellOutNumber);
    }

    public void updateSkuSellOut(int skuID, int sellNumber) {
        gDAO.updateSkuSellOut(skuID, sellNumber);
    }

    public List<ThSku> getPrice(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.getPrice(goodsID);
    }

    public Brand getBrand(int brandID) {
        // TODO Auto-generated method stub
        return gDAO.getBrand(brandID);
    }

    public boolean remind(int goodsID, int type) {
        return gDAO.remind(goodsID, type);
    }

    public int remindNumber(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.remindNumber(goodsID);
    }

    public int isCollection(int userID, int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.isCollection(userID, goodsID);
    }

    public AutoReply getAutoReply(int shopID) {
        // TODO Auto-generated method stub
        return gDAO.autoReply(shopID);
    }

    public int commentNumber(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.commentNumber(goodsID);
    }

    public List<?> goodsRating(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.goodsRating(goodsID);
    }

    public int getShopID(int type, String province, String city, String county) {
        // TODO Auto-generated method stub
        if (county == null || county.equals("") || county.equals("null")) {
            county = null;
        }
        return gDAO.getShopID(type, province, city, county);
    }

    public ThSku goodsPrice(int goodsID, String format) {
        // TODO Auto-generated method stub
        return gDAO.getGoodsPrice(goodsID, format);
    }

    public ThSku getGoodsPrice(int skuID) {
        return gDAO.getGoodsPrice(skuID);
    }

    public List<Broadcast> castList() {
        // TODO Auto-generated method stub
        return gDAO.castList();
    }

    public ModelPicture getModelPicture() {
        // TODO Auto-generated method stub
        return gDAO.getModelPicture();
    }

    public PrePay getPrepay(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.getPrepay(goodsID);
    }

    public ActivityPrice getActivityPrice(int activityID, int goodsID, String format) {
        // TODO Auto-generated method stub
        return gDAO.getActivityPrice(activityID, goodsID, format);
    }

    public BussinessInfo getBussinessInfo(int bussinessID) {
        // TODO Auto-generated method stub
        return gDAO.getBussinessInfo(bussinessID);
    }

    public int goodComment(int goodsID) {
        // TODO Auto-generated method stub
        return gDAO.goodComment(goodsID);
    }

    public ActivityPrice getActivityPrice(int goodsID, String format) {
        // TODO Auto-generated method stub
        return gDAO.getActivityPrice(goodsID, format);
    }

    public List<HotCity> recommentCityList(String city, String county) {
        // TODO Auto-generated method stub
        return gDAO.recommendCityList(city, county);
    }

    public List<Goods> popularGoodsGet(String province, String city, String county, PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.popularGoodsGet(province, city, county, page);
    }

    public List<ThGroupBuying> groupBuyingGroupGet(int groupRulesId, int num) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingGroupGet(groupRulesId, num);
    }

    public ThGroupBuyingRules groupBuyingRuleGet(int groupRulesId) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingRuleGet(groupRulesId);
    }

    public ClientInfo[] groupBuyingUsersGet(int userID, int skuID) {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean joinGroupBuyingGoup(int userID, int groupID, int skuID, int num) {
        // TODO Auto-generated method stub
        return false;
    }

    public BussinessInfo getBusinessByCounty(String countyID) {
        // TODO Auto-generated method stub
        return gDAO.getBusinessByCounty(countyID);
    }

    public List<ThCategories> getCateListByParentAndBussiness(int parentID, int bussinessID,
                                                              PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.getCateListByParentAndBussiness(parentID, bussinessID, page);
    }

    /**
     * 获取一级分类下面的所有二级分类
     * @return
     */
    public List<ThJsonCategory> getCategoryList(int cateId) {
        List<?> list = gDAO.categoriesGet(cateId, 0);
        List<ThJsonCategory> cateList = new ArrayList<ThJsonCategory>();
        for (int j = 0; j < list.size(); j++) {
            Object[] obj = (Object[]) list.get(j);
            ThJsonCategory cate = new ThJsonCategory();
            cate.setCateId(Integer.parseInt(obj[0].toString()));
            /*if (!gDAO.isCateHavingGoods(cate.getCateId())) {
                continue;
            }*/
            cate.setParentId(Integer.parseInt(obj[1].toString()));
            cate.setCateName(obj[2].toString());
            cate.setKeywords(obj[4].toString());
            cate.setCateDesc(obj[5].toString());
            if (obj[6] != null) {
                cate.setThumbnail(GoodsParse.getOneGoodsPicture(obj[6].toString()));
            }
            cate.setRedirectTo(obj[7].toString());
            if (obj[8] != null) {
                cate.setMaskingLayer(GoodsParse.getOneGoodsPicture(obj[8].toString()));
            }
            //cate.setMaskingLayerPosi(obj[9].toString());
            cateList.add(cate);
        }
        return cateList;
    }

    public List<ThJsonCateGroup> categoryTreeGet(int shopId) {
        List<ThCateGroup> groupList = gDAO.cateGroupGet();
        if (groupList == null) {
            return null;
        }

        //		List<?> validCateIds = gDAO.cateListInShop(shopId);

        List<ThJsonCateGroup> tree = new ArrayList<ThJsonCateGroup>();
        for (int i = 0; i < groupList.size(); i++) {
            ThCateGroup group = (ThCateGroup) groupList.get(i);
            ThJsonCateGroup jsonGroup = new ThJsonCateGroup();
            tree.add(jsonGroup);
            jsonGroup.setGroupId(group.getId());
            jsonGroup.setTitle(group.getTitle());
            jsonGroup.setIcon(GoodsParse.getOneGoodsPicture(group.getIcon()));

            List<?> list = gDAO.categoriesGet(group.getId(), shopId);
            if (list == null) {
                continue;
            }
            List<ThJsonCategory> cateList = new ArrayList<ThJsonCategory>();
            for (int j = 0; j < list.size(); j++) {
                Object[] obj = (Object[]) list.get(j);
                ThJsonCategory cate = new ThJsonCategory();
                cate.setCateId(Integer.parseInt(obj[0].toString()));
                if (!gDAO.isCateHavingGoods(cate.getCateId())) {
                    continue;
                }
                cate.setParentId(Integer.parseInt(obj[1].toString()));
                cate.setCateName(obj[2].toString());
                cate.setKeywords(obj[4].toString());
                cate.setCateDesc(obj[5].toString());
                if (obj[6] != null) {
                    cate.setThumbnail(GoodsParse.getOneGoodsPicture(obj[6].toString()));
                }
                cate.setRedirectTo(obj[7].toString());
                //cate.setMaskingLayer(GoodsParse.getOneGoodsPicture(obj[8].toString()));
                //cate.setMaskingLayerPosi(obj[9].toString());
                if (shopId != 0) {
                    cate.setCateImg(obj[10] == null ? "" : GoodsParse.getOneGoodsPicture(obj[10]
                        .toString()));
                }

                // 判断分类内是否存在内容，不存在则不返回
                //				if (cate.getRedirectTo().equals("1")) {
                //					boolean delete = true;
                //					for (int q = 0; q < validCateIds.size(); q++) {
                //						Object[] validId = (Object[]) validCateIds.get(q);
                //						if (Integer.parseInt(validId[0].toString()) == cate.getCateId()) {
                //							delete = false;
                //							break;
                //						}
                //					}
                //					if (delete) {
                //						continue;
                //					}
                //				} else if (cate.getRedirectTo().equals("2")){
                //					List<?> goodsList = gDAO.goodsList(shopId, cate.getCateId(), new PageUtil(1));
                //					if (goodsList.size() == 0 && shopId != 0) {
                //						continue;
                //					}
                //				}

                cateList.add(cate);
            }

            jsonGroup.setCategories(cateList);
        }

        return tree;
    }

    public List<?> getGoodsBySubCate(int cateID) {
        // TODO Auto-generated method stub
        return null;
    }

    public void compareImg() {
        gDAO.compareImg();
        return;
    }

    public List<ThCategories> getSubCateByRootCate(int cateID) {
        // TODO Auto-generated method stub
        return null;
    }

    public List<?> getGoodsBySubCates() {
        // TODO Auto-generated method stub
        return null;
    }

    public List<?> getGoodsBySubCates(List<Integer> cateIDs) {
        // TODO Auto-generated method stub
        return null;
    }

    public List<?> groupBuyingActivityList(PageUtil page, String directId, int groupType) {
        return gDAO.groupBuyingActivityList(page, directId, groupType);
    }

    public List<ThGroupBuyingRules> groupBuyingGroupRulesGet(PageUtil page) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingGroupRulesGet(page);
    }

    public ThSku skuGet(int skuId) {
        // TODO Auto-generated method stub
        return gDAO.skuGet(skuId);
    }

    public ThGroupBuying groupBuyingGet(int groupRulesId, int userId) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingGet(groupRulesId, userId);
    }

    public ThGroupBuying groupBuyingGetByRulesId(int groupRulesId) {
        return gDAO.groupBuyingGetByRulesId(groupRulesId);
    }

    public ThGroupBuying groupBuyingGet(int groupId) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingGet(groupId);
    }

    public boolean groupBuyingUpdate(ThGroupBuying group) {
        // TODO Auto-generated method stub
        return gDAO.groupBuyingUpdate(group);
    }

    public List<?> groupByingHistoryGet(int groupId) {
        // TODO Auto-generated method stub
        return gDAO.groupByingHistoryGet(groupId);
    }

    public List<ThJsonTopics> topicsGet(int shopId, int categoryId) {
        List<?> list = gDAO.topicsGet(shopId, categoryId);
        List<ThJsonTopics> topicList = new ArrayList<ThJsonTopics>();

        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);
            ThJsonTopics topic = new ThJsonTopics();

            int topicId = Integer.parseInt(obj[0].toString());
            if (!gDAO.isShowTopic(topicId)) {
                continue;
            }
            topic.setTopicId(topicId);
            topic.setTitle(obj[1].toString());
            topic.setSubtitle(obj[2].toString());
            topic.setDescription(obj[3].toString());
            topic.setState(Integer.parseInt(obj[4].toString()));
            topic.setImage(GoodsParse.getOneGoodsPicture(obj[5].toString()));
            topic.setStartAt(((Date) obj[6]).getTime());
            topic.setEndAt(((Date) obj[7]).getTime());

            float floorPrice = gDAO.topicGoodsFloorPrice(topic.getTopicId());
            topic.setFloorPrice(floorPrice);

            topicList.add(topic);
        }
        return topicList;
    }

    public List<?> getTopics(String topicsType, int shopId, String state, String startTime,
                             String endTime, PageUtil page) {
        return gDAO.getTopics(topicsType, shopId, state, startTime, endTime, page);
    }

    public ThJsonRemoteTopic topicGet(int topicId,int shopId) {
        Object[] topicObj = (Object[]) gDAO.topicGet(topicId);

        ThJsonRemoteTopic topic = new ThJsonRemoteTopic();
        topic.setTopicId(topicId);
        topic.setTitle(topicObj[0].toString());
        topic.setSubtitle(topicObj[1].toString());
        topic.setDescription(topicObj[2].toString());
        topic.setTopicImage(GoodsParse.getOneGoodsPicture(topicObj[3].toString()));
        if (topicObj[4] == null) {
            topic.setShopId(0);
        } else {
            topic.setShopId(Integer.parseInt(topicObj[4].toString()));
        }
        if (topicObj[5] == null) {
            topic.setShopLogo("");
        } else {
            topic.setShopLogo(Path.nginxServerBusinessDir + Path.SHOP + File.separator + Path.LOGO
                              + File.separator + topicObj[5].toString());
        }

        int cateId = Integer.parseInt(topicObj[6].toString());
        if (cateId == 0) {
            return null;
        }

        List<?> list = gDAO.topicGoodsGet(topicId,shopId);

        List<JsonGoodsList> goodsList = new ArrayList<JsonGoodsList>();
        for (int i = 0; i < list.size(); i++) {
            String goodsFormat;
            Object[] obj = (Object[]) list.get(i);
            JsonGoodsList jGoods = new JsonGoodsList();

            jGoods.setGoodsID(Integer.parseInt(obj[0].toString()));
            jGoods.setGoodsPrice(Float.parseFloat(obj[1].toString()));
            jGoods.setGoodsPic(GoodsParse.getOneGoodsPicture(obj[2].toString()));

            List<ThSku> skuList = gDAO.getSkusByGoodsID(obj[0].toString());

            jGoods.setStoreNumber(skuList.get(0).getStock());
            jGoods.setSellNumber(skuList.get(0).getSellout() + skuList.get(0).getFakeSellout());
            //董事特权只能享受分润总金额的90%
            if (skuList.get(0).getProfitShare() >= 0) {
                int profitShare = (int) (skuList.get(0).getProfitShare() * 0.9);
                jGoods.setProfitShare(profitShare);
            }

            jGoods.setShopID(Integer.parseInt(obj[5].toString()));
            jGoods.setShopName((obj[6] == null) ? null : obj[6].toString());
            jGoods.setGoodsBFPrice(Float.parseFloat(obj[7].toString()));
            jGoods.setGoodsTitle((obj[8] == null) ? null : obj[8].toString());
            jGoods.setGoodsLabel((obj[9] == null) ? null : obj[9].toString());

            goodsFormat = (obj[10] == null) ? null : obj[10].toString();
            ActivityPrice ap = getActivityPrice(Integer.parseInt(obj[0].toString()), goodsFormat);
            if (ap != null) {
                jGoods.setGoodsPrice(ap.getStartPrice());//活动价格
                jGoods.setGoodsBFPrice(ap.getEndPrice());//还原的价格
            }

            jGoods.setPercent((obj[11] == null) ? 0 : Float.parseFloat(obj[11].toString()));
            jGoods.setStartTime((obj[12] == null) ? null : obj[12].toString());
            jGoods.setEndTime((obj[13] == null) ? null : obj[13].toString());
            jGoods.setDeliveryTime((obj[14] == null) ? null : obj[14].toString());

            jGoods.setRemindNumber(remindNumber(Integer.parseInt(obj[0].toString())));

            goodsList.add(jGoods);
        }

        topic.setGoods(goodsList);

        return topic;
    }

    public List<Goods> wholesaleGoodsGet(String goodsType, String province, String city,
                                         String county, int type, PageUtil page) {
        // TODO Auto-generated method stub
        List<Goods> goodsList = gDAO.wholesaleGoodsGet(goodsType, province, city, county, type,
            page);
        return goodsList;
    }

    public List<Goods> convertSkuData(List<Goods> goodsList) {
        if (goodsList != null && goodsList.size() > 0) {
            int goodsIDs[] = new int[goodsList.size()];
            for (int i = 0; i < goodsList.size(); i++) {
                goodsIDs[i] = goodsList.get(i).getGoodsID();
            }
            List<ThSku> skuList = gDAO.getSkusByGoodsIDs(goodsIDs);
            for (Goods goods : goodsList) {
                goods.setThSkus(new ArrayList<ThSku>());
                for (ThSku sku : skuList) {
                    if (sku.getGoodsID() == goods.getGoodsID())
                        goods.getThSkus().add(sku);
                }
            }
        }
        return goodsList;
    }

    public List<ThBanners> bannerListGet() {
        return gDAO.bannerListGet();
    }

    public List<?> cateListInShop(int shopId) {
        return gDAO.cateListInShop(shopId);
    }

    public List<?> recommentCityListGet(String city, String county) {
        // TODO Auto-generated method stub
        return gDAO.recommendCityListGet(city, county);
    }

    public List<?> getGoodsByCateIdAndShopId(int cateId, int shopId, PageUtil page) {
        return gDAO.getGoodsByCateIdAndShopId(cateId, shopId, page);
    }

    public List<?> goodsListByShopIdList(List<Integer> shopIdList, PageUtil page) {
        return gDAO.goodsListByShopIdList(shopIdList, page);
    }

    public List<?> getGoodsListByShopId(int shopId, PageUtil page) {
        return gDAO.goodsListByShopId(shopId, page);
    }

    public List<?> getGoodsListWithSkusByShopId(int shopId, PageUtil page) {
        return null;
    }

    public List<Integer> getShopIdByAreaName(String province, String city, String country) {
        return gDAO.getShopIdByAreaName(province, city, country);
    }

    public int shopIdGet(String province, String city, String country) {
        return gDAO.shopIdGet(province, city, country);
    }

    public boolean updateTopicState(int state, int topicId) {
        return gDAO.updateTopicState(topicId, state);
    }

    public List<String> getAllImages(PageUtil page) {
        return gDAO.getAllImages(page);
    }

    public boolean reduceSkuStock(int skuId, int number) {
        return gDAO.reduceSkuStock(skuId, number);
    }

    public void updateGoodsStock(int goodsID) {
        gDAO.updateGoodsStock(goodsID);
    }

    public List<?> goodsListByType(final int type, final PageUtil page) {
        return gDAO.goodsListByType(type, page);
    }

    public int getGoodSaleHisAmount(int goodId) {
        return gDAO.getGoodSaleHisAmount(goodId);
    }
}
